<?php
require_once 'BaseController.php';

/**
 * 【PC版】最近みた物件 コントローラ
 */
class Renew_RecentController extends Renew_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // 過去の日付
        $params = $this->getParameters();
        $recentList = $_COOKIE['recentList'];

        if (!isset($recentList) || empty($recentList) || $recentList == '')
        {
            $this->_data['NoData'] = true;
            $this->displayBody2('index');
            return;
        }

        $model = new Renew_Model_BukkenList();
        $model_conv = new Rstobi_Model_Converte();
        $model_common = new Rstobi_Model_Common();

        $from = $model_common->getCampFrom($params);

        $dbParams = array();

        $recentList = str_replace('*undefined,', '', $recentList);
        $recentList = str_replace('undefined,', '', $recentList);
        $recentList = trim(str_replace(undefined, '', $recentList));

        $recents = array();

        $vals = explode(',', $recentList);
        foreach ($vals as $val)
        {
            if (isset($val) && !empty($val) && trim($val) != '')
            {
                $recents[] = $val;
            }
        }
        $recentString = implode(',', $recents);

        $dbParams['MERGE_EXCLUDE'] = true;
        $data = $model->getBukenByCds($recentString, $from, $dbParams);
        $tatecds = [];
        foreach ($recents as $heyaCd)
        {
            foreach ($data as $d)
            {
                if ($d['T03_HEYA_CD'] == $heyaCd && !in_array($d['T03_TATEMONO_CD'], $tatecds))
                {
                    $tatecds[] = $d['T03_TATEMONO_CD'];
                    break;
                }
            }
        }
        $bukken = [];
        foreach ($tatecds as $tatecd)
        {
            foreach ($data as $d)
            {
                if ($d['T03_TATEMONO_CD'] === $tatecd)
                {
                    $bukken[] = $d;
                }
            }
        }
        if (!isset($bukken) || sizeof($bukken) == 0)
        {
            $this->_data['NoData'] = true;
            $this->displayBody2('index');
            return;
        }

        $model_conv->dataConverteForBuken($bukken, $params, false, false);

        $prev = NULL;
        $c = -1;
        foreach ($bukken as $d)
        {
            $tatemono = $d['T03_DAIHYO_TATEMONO_CD'];
            if ($prev != $tatemono)
            {
                $c++;
                $items[] = array();
            }
            $d['MAX_CHINRYO_STR'] = (intval($d['MAX_CHINRYO']) / 10000);
            $d['MIN_CHINRYO_STR'] = (intval($d['MIN_CHINRYO']) / 10000);
            // OEM判定
            $OemFlg = ((array_key_exists('OemCd', $params) == true) && (is_null($params['OemCd']) == false) && (is_numeric($params['OemCd']) == true));
            // 物件画像取得
            $d['BUKEN_IMAGE'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
            $d['FULLGAZOU_ICON'] = Apm_Bukkenitems::GetFullGazouIcon($d['BUKEN_IMAGE']);
            // キャンペーン取得
            $heyaCd = $d['T03_HEYA_CD'];
            $ret = $model->getCamp($heyaCd);
            if (!empty($ret))
            {
                $d['M94_ITEM_ICON'] = $ret['M94_ITEM_ICON'];
                $d['M94_CAMPAIGN_URL'] = $ret['M94_CAMPAIGN_URL'];
                $d['M94_CAMPAIGN_NAME'] = $ret['M94_CAMPAIGN_NAME'];
            }

            $items[$c][] = $d;
            $prev = $tatemono;
        }

        $this->_data['list'] = $items;

        $this->displayBody2('index');
    }

    /**
     * SEO対策用のmetaタグに配置する文章を設定
     */
    protected function setSeoMeta()
    {
        $this->_meta_title = '最近見た物件から賃貸物件を探す 【アパマンショップ】';
        $this->_meta_keywords = '各種お問い合わせ｜';
    }

}
?>